using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Commerce.Common;
using System.Collections.Generic;
using System.Data.Common;
using SubSonic;

/// <summary>
///  Business logic for categories
/// </summary>
public static class CategoryController
{
    
    private static CategoryCollection catList;

    /// <summary>
    /// This is the static list for the site's categories. This is held in application
    /// memory and used as needed so the site doesn't need to hit the DB everytime. Will
    /// need to opt out of this if you use a server farm or cluster.
    /// </summary>
    public static CategoryCollection CategoryList
    {
        get { return catList; }
        set { catList = value; }
    }
	
    /// <summary>
    /// Loads the category info into the static CategoryList
    /// </summary>
    public static void Load()
    {
        catList = new CategoryCollection();
        IDataReader rdr = Category.FetchAll(OrderBy.Asc("listOrder"));
        catList.Load(rdr);
        rdr.Close();

    }

    /// <summary>
    /// Find a category in the CategoryList
    /// </summary>
    /// <param name="categoryID"></param>
    /// <returns></returns>
    public static Category Find(int categoryID)
    {
        Category cOut = null;
        foreach (Category cat in catList)
        {
            if (cat.CategoryID == categoryID)
            {
                cOut = cat;
                break;
            }
        }
        return cOut;
    }

    /// <summary>
    /// Finds a category by name
    /// </summary>
    /// <param name="categoryName"></param>
    /// <returns></returns>
    public static Category FindByName(string categoryName)
    {
        Category cOut = null;
        foreach (Category cat in catList)
        {
            if (cat.CategoryName.ToLower().Equals(categoryName))
            {
                cOut = cat;
                break;
            }
        }
        return cOut;
    }

    /// <summary>
    /// Returns all categories in a dataset; used by the
    /// menuing controls
    /// </summary>
    /// <returns></returns>
    public static DataSet GetDataSetList()
    {
        //Query q = new Query(Category.GetTableSchema());
        Query q = new Query(Category.Schema);
        //return q.ExecuteDataSet();
        return q.ExecuteDataSet();
    }

    /// <summary>
    /// Returns all categories for a given product
    /// </summary>
    /// <param name="productID"></param>
    /// <returns>CategoryCollection</returns>
    public static CategoryCollection GetByProductID(int productID)
    {
        IDataReader rdr = SPs.StoreCategoryGetByProductID(productID).GetReader();
        CategoryCollection list = new CategoryCollection();
        list.Load(rdr);
        rdr.Close();
        return list;
    }

    /// <summary>
    /// Returns a multiple-result set for populating the Category.aspx page. Includes
    /// category info, products, etc.
    /// </summary>
    /// <param name="categoryGUID"></param>
    /// <returns></returns>
    public static DataSet GetPageByName(string categoryName)
    {
        return SPs.StoreCategoryGetPageByNameMulti(categoryName).GetDataSet();

    }
    /// <summary>
    /// Returns a multiple-result set for populating the Category.aspx page. Includes
    /// category info, products, etc.
    /// </summary>
    /// <param name="categoryGUID"></param>
    /// <returns></returns>
    public static DataSet GetPageByID(int categoryID)
    {
         
        return SPs.StoreCategoryGetPageMulti(categoryID).GetDataSet();

    }
    /// <summary>
    /// Returns a multiple-result set for populating the Category.aspx page. Includes
    /// category info, products, etc.
    /// </summary>
    /// <param name="categoryGUID"></param>
    /// <returns></returns>
	public static DataSet GetPageByGUID(string categoryGUID) {

        return SPs.StoreCategoryGetPageByGUIDMulti(categoryGUID).GetDataSet();
	}
}
